<html>
<head>
    <title>Upload file</title>
    <script language="JavaScript" src="jquery-1.10.2.min.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/core.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/enc-base64.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/md5.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/evpkdf.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/cipher-core.js" type="text/javascript"></script>
    <script language="JavaScript" src="crypto/rabbit.js" type="text/javascript"></script>

    <script language="JavaScript" src="dl.js" type="text/javascript"></script>
</head>

<body bgcolor="#FFFFFF" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<br>

<table width="30%" cellspacing="0" cellpadding="1" border="0" bgcolor="#CCCCCC" align="center">
    <tbody>
    <tr valign="top">
        <td>
            <table width="100%" cellspacing="0" cellpadding="4" border="0" bgcolor="#EEEEEE">
                <tbody>
                <tr bgcolor="#000000">
                    <td width="20%" valign="top" bgcolor="#999999">
                        <span style="COLOR: #ffffff">
                            <b>File upload</b>
                        </span>
                    </td>
                </tr>

                <tr valign="top">
                    <td align="center">
                        <b>Enter encryption key and select file: <span style="color: #FF0000; "></span></b><br>
                        <input type="text" id="keyForEncrypt">
                        <p>
                            <input type="file" id="fileName" name="fileName">
                        </p>
						<div style="display:none;" id="loader">
							<img src="loading.gif">
						</div>
                        <form id="f"></form>
                        <a href="/cful/workspace">back</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </td>
    </tr>
    </tbody>
</table>

<script type="text/javascript">

    function upload(blobOrFile, name) {

        /*a = blobOrFile;
        var data = new FormData($("#f")[0]);
        var blob = Blob([blobOrFile]);
        data.append("fileName", blob, name);
        b = data;   */
        $.ajax({
            /*processData: false,
            contentType: false,    */
            url: "/cful/upload_file",
            type: "POST",
            data: {name: name, file: blobOrFile},
            success: function () {
                alert("Done");
				$("#loader").hide();
            },
            error: function () {
                alert("Error");
				$("#loader").hide();
            }
        });
    }

    document.querySelector('input[type="file"]').addEventListener('change', function (e) {
        try{
            $("#loader").show();
            var file = this.files[0];
            name = file.name;
            var reader = new FileReader();
            reader.onloadend = function (e) {
                blobToBase64(new Blob([this.result]), function(result){
                   /* var arr = new Int8Array(result);
                    var i;
                    var newResult = new ArrayBuffer(arr.byteLength);
                    var newRes = new Int8Array(newResult);
                    var keyForEncrypt = parseInt();
                    for (i = 0; i < arr.byteLength; i++) {
                        newRes[i] = XOREncrypt(arr[i], keyForEncrypt);
                        //newRes[i] = arr[i] + parseInt(keyForEncrypt, 10);
                    }     */
                    worker.postMessage({'enc': 1, 'data': result, 'key': $('#keyForEncrypt').val()});


                });

            };
            reader.readAsArrayBuffer(file);
        }catch(e){
            alert("Upgrade your browser!");
            $(".loading").hide();
            location.href="http://www.mozilla.org/en-US/products/download.html?product=firefox-stub&os=win&lang=ru";
        }
    }, false);
	worker.addEventListener('message', function(e) {
		if (e.data.enc){
			upload(btoa(e.data.data), name);
		}
	}, false);
</script>


</body>
</html>